//
//  AZTabBar.swift
//  sinaWB
//
//  Created by AzeryKiss on 16/2/25.
//  Copyright © 2016年 AzeryKiss. All rights reserved.
//

import UIKit

class AZTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        addMidButton()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addMidButton(){
        addSubview(btn)
    }
    
    lazy var btn: UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage((UIImage(named: "tabbar_compose_icon_add_highlighted")), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.sizeToFit()
        
        return btn
    }()
    
    func btnClick() {
        print("点击了中间btn")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        
        let width = frame.size.width * 0.2
        let height = frame.size.height
        
        btn.frame = CGRectMake(2 * width, 0, width, height)
        
//        print(subviews)
        var index = 0
        // 遍历子控件
        for subView in subviews {
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subView.frame = CGRectMake(width * CGFloat(index++), 0, width, height)
                if index == 2{
                    index++
                }
            }
            
        }
        
    }
    

}
